<?php


namespace app\admin\controller;


use think\Controller;
use think\facade\Request;
use think\facade\View;
use app\common\model\Apply as ApplyModel;
use think\Db;
class Apply extends Base
{

    public function index(){
        $where   = [];
        $keyword = Request::param('keyword');
        if (!empty($keyword)) {
            $ids = [];
            $project = model('project')->where('title','like','%'.$keyword.'%')->field('id')->select()->toArray();
            if($project) {
                foreach ($project as $val) {
                    $apply_id =model('apply')->where('project_id',$val['id'])->select()->toArray();
                    if ($apply_id){
                        foreach ($apply_id as $apply_id_value){
                            array_push($ids, $apply_id_value['id']);
                        }
                    }
                }
            }
            $user_id = model('users')->where('vol_true_name','like','%'.$keyword.'%')->field('id')->select()->toArray();
            if($user_id){
                foreach ($user_id as $user_id_val){
                    $apply_id =model('apply')->where('user_id',$user_id_val['id'])->select()->toArray();
                    if ($apply_id){
                        foreach ($apply_id as $apply_id_value){
                            array_push($ids, $apply_id_value['id']);
                        }
                    }
                }
            }
            if ($ids)
            {
                //数组去重
               $ids =  array_unique($ids);
            }
            $where[] = ['id', 'in', $ids];
        }
        $dateran = Request::param('dateran');
        if (!empty($dateran)) {
            $getDateran = get_dateran($dateran);
            $where[]    = ['create_time', 'between', $getDateran];
        }
        $list = ApplyModel::with('getProject,getUser')->where($where)->order(['id' => 'desc'])->paginate($this->pageSize, false, ['query' => Request::get()]);
        $all_articleNum = model('Apply')->select();
        $view = [
            'keyword'  => $keyword,
            'dateran'  => $dateran,
            'pageSize' => page_size($this->pageSize, $list->total()),
            'list'     => $list,
            'page'     => $list->render(),
            'empty'    => empty_list(9),
            'all_order_num' => $all_articleNum
        ];
        View::assign($view);
        return view();
    }

    //删除
    public function del()
    {
        $id = Request::param('id');
        ApplyModel::destroy($id);
        return json(['error' => 0, 'msg' => '删除成功']);
    }

    //批量删除
    public function selectDel()
    {
        $id = Request::param('id');
        ApplyModel::destroy($id);
        return json(['error' => 0, 'msg' => '删除成功!']);
    }

    public function edit($id=0){
        if(request()->isAjax()){
            $data = input('post.');
            $result = model('Apply')->where('id',input('post.id'))->update($data);
            if($result)
            {
                $this->success('修改成功',$_SERVER['HTTP_REFERER']);
            }else{
                $this->error('修改失败!',$_SERVER['HTTP_REFERER']);
            }
        }
        $ApplyInfo = model('Apply')::with('getProject,getUser')->find($id);
        $viewData = [
            'Apply'=>$ApplyInfo
        ];
        $this->assign($viewData);
        return view();
    }
}
